package net.maku.system.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.storage.service.StorageService;
import net.maku.system.vo.SysFileUploadVO;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.ByteArrayEntity;

/**
 * 文件上传
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("sys/file")
@Tag(name = "文件上传")
@AllArgsConstructor
public class SysFileUploadController {
    private final StorageService storageService;

    @PostMapping("upload")
    @Operation(summary = "上传")
    @Transactional
    @OperateLog(type = OperateTypeEnum.INSERT)
    public Result<SysFileUploadVO> upload(@RequestParam("file") MultipartFile file,String openid) throws Exception {
        if (file.isEmpty()) {
            return Result.error("请选择需要上传的文件");
        }

        // 上传路径
        String path = storageService.getPath(file.getOriginalFilename());
        // 上传文件
        String url = storageService.upload(file.getBytes(), path);

        SysFileUploadVO vo = new SysFileUploadVO();
        vo.setUrl(url);
        vo.setSize(file.getSize());
        vo.setName(file.getOriginalFilename());
        vo.setPlatform(storageService.properties.getConfig().getType().name());

//        String result= HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxeb51ae4d4753d3bd&secret=74706deff320b23a81c3b24f7a3f4b57");
//        String access_token = JSON.parseObject(result).getString("access_token");
//        try (InputStream inputStream = file.getInputStream()) {
//            String url2 = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=" + access_token;
//            HttpClient httpclient = HttpClients.createDefault();
//            HttpPost request = new HttpPost(url2);
//            request.addHeader("Content-Type", "application/octet-stream");
//            byte[] byt = new byte[inputStream.available()];
//            inputStream.read(byt);
//            request.setEntity(new ByteArrayEntity(byt, ContentType.create("image/jpg")));
//            HttpResponse response = httpclient.execute(request);
//            HttpEntity entity = response.getEntity();
//            String result2 = EntityUtils.toString(entity, "UTF-8");// 转成string
//            String result3 = String.valueOf(JSONObject.parseObject(result2).get("errmsg"));
//            vo.setErrmsg(result3);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        return Result.ok(vo);
    }

    @PostMapping("uploads")
    @Operation(summary = "上传")
    @OperateLog(type = OperateTypeEnum.INSERT)
    public SysFileUploadVO uploads(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new ServerException("请选择需要上传的文件");
        }

        // 上传路径
        String path = storageService.getPath(file.getOriginalFilename());
        // 上传文件
        String url = storageService.upload(file.getBytes(), path);

        SysFileUploadVO vo = new SysFileUploadVO();
        vo.setUrl(url);
        vo.setName(file.getOriginalFilename());

        return vo;
    }
}
